/***********初始化**********/
function initper(fjid,sim,isFirst)
{
	GX(fjid,sim);
	ZZ(fjid,sim);
	YWZC(fjid,sim);
	if(isFirst){
		MC(fjid,sim);	
	}
}

/*******功效**********/
function GX(fjid,sim){
	var url = "app/pre/simPer.do?q="+fjid+"&df=id&type=GX&sim="+sim;
	clearRow("GXT");
	perAjax(url,{},function (request){
		var obj = eval("["+request.responseText+"]");
		addData("GXT",obj);
	},faiFn);

}

/*******主治**********/
function ZZ(fjid,sim){
	var url = "app/pre/simPer.do?q="+fjid+"&df=id&type=ZZ&sim="+sim;
	clearRow("ZZT");
	perAjax(url,{},function (request){
		var obj = eval("["+request.responseText+"]");
		addData("ZZT",obj);
	},faiFn);
}

/*******药物组成**********/
function YWZC(fjid,sim){
	var url = "app/pre/simPer.do?q="+fjid+"&df=id&type=YWZC&sim="+sim;
	clearRow("YWZCT");
	perAjax(url,{},function (request){
		var obj = eval("["+request.responseText+"]");
		addData("YWZCT",obj);
	},faiFn);
}

/*******名称**********/
function MC(fjid,sim){
	var url = "app/pre/simPer.do?q="+fjid+"&df=id&type=MC&sim="+sim;
	clearRow("MCT");
	perAjax(url,{},function (request){
		var obj = eval("["+request.responseText+"]");
		addData("MCT",obj);
	},faiFn);
}

function perAjax(url,params,sucFn,faiFn){
	Ext.Ajax.request({
	   url: url,
	   success: sucFn,
	   failure: faiFn,
	   params: params
	});
}

function faiFn(){
alert("加载失败！");
}
function clearRow(tableName){ 
 var objTable= document.getElementById(tableName); 
 var size = objTable.rows.length;
	for( var i=0; i<size ; i++ ) 
	{
		objTable.deleteRow(0); 
	} 
	addData(tableName,null);
} 

function addData(tableName,data){
   if(data == null || data[0]==null){
   		return;
   }   
   
 //  alert("return sim data size:"+size+" table name:"+tableName+" data "+data);
 
 	if(tableName=="MCT"){
 		addMCdata(data);
 	}else if(tableName=="YWZCT"){
 		addYWZCdata(data);
 	}else if(tableName=="GXT"){
 		addGXdata(data);
 	}else if(tableName=="ZZT"){
 		addZZdata(data);
 	}	
}

function addMCdata(data){
	var size = data[0].length;
	var testTbl= document.getElementById("MCT"); 
	document.getElementById("MCnumber").value=size; 
	var newTr = testTbl.insertRow(testTbl.rows.length);
	var newTd; 
	for(var i=0;i<size;i++)
   {				
		newTd = newTr.insertCell(); 		
		//newTd.innerHTML=""+data[0][i].fangm;
		var detail="方名："+data[0][i].fangm+"\n";
		detail+="处方来源："+data[0][i].chufly+"\n";
		detail+="药物组成："+data[0][i].yaowzc+"\n";
		detail+="功效："+data[0][i].gongx+"\n";
		detail+="主治："+data[0][i].zhuz+"\n";
		detail+="用法用量："+data[0][i].yongfyl+"\n";
		
		newTd.innerHTML="<a href='/pre/app/pre/search.do?type=simila&q="+data[0][i].id+"&df=id' title='"+detail+"'>"+data[0][i].fangm;
		if((i+1)%10==0){
			newTr = testTbl.insertRow(testTbl.rows.length);
		}
	}
}

function addYWZCdata(data){
	var size = data[0].length;
	var testTbl= document.getElementById("YWZCT"); 
	document.getElementById("YWZCnumber").value=size; 
	for(var i=0;i<size;i++){		
		var newTr = testTbl.insertRow(testTbl.rows.length);
		var newTd = newTr.insertCell(); 
		
		var detail="方名："+data[0][i].fangm+"\n";
		detail+="处方来源："+data[0][i].chufly+"\n";
		detail+="功效："+data[0][i].gongx+"\n";
		detail+="主治："+data[0][i].zhuz+"\n";
		detail+="用法用量："+data[0][i].yongfyl+"\n";
		
		newTd.innerHTML="<a href='/pre/app/pre/search.do?type=simila&q="+data[0][i].id+"&df=id' title='"+detail+"'>"+data[0][i].fangm;
		
		newTd = newTr.insertCell(); 
		newTd.innerHTML=""+data[0][i].yaowzc;
	}
}

function addGXdata(data){
	var size = data[0].length;
	var testTbl= document.getElementById("GXT"); 
	document.getElementById("GXnumber").value=size; 
	for(var i=0;i<size;i++){		
		var newTr = testTbl.insertRow(testTbl.rows.length);
		var newTd = newTr.insertCell(); 
		
		var detail="方名："+data[0][i].fangm+"\n";
		detail+="处方来源："+data[0][i].chufly+"\n";
		detail+="药物组成："+data[0][i].yaowzc+"\n";
		detail+="主治："+data[0][i].zhuz+"\n";
		detail+="用法用量："+data[0][i].yongfyl+"\n";
		newTd.innerHTML="<a href='/pre/app/pre/search.do?type=simila&q="+data[0][i].id+"&df=id' title='"+detail+"'>"+data[0][i].fangm;
		
		newTd = newTr.insertCell(); 
		if(data[0][i].gongx!=null){
			newTd.innerHTML=""+data[0][i].gongx;
		}
	}
}

function addZZdata(data){
	var size = data[0].length;
	var testTbl= document.getElementById("ZZT"); 
	document.getElementById("ZZnumber").value=size; 
	for(var i=0;i<size;i++){		
		var newTr = testTbl.insertRow(testTbl.rows.length);
		var newTd = newTr.insertCell(); 
		
		var detail="方名："+data[0][i].fangm+"\n";
		detail+="处方来源："+data[0][i].chufly+"\n";
		detail+="药物组成："+data[0][i].yaowzc+"\n";
		detail+="功效："+data[0][i].gongx+"\n";
		detail+="用法用量："+data[0][i].yongfyl+"\n";
		
		newTd.innerHTML="<a href='/pre/app/pre/search.do?type=simila&q="+data[0][i].id+"&df=id' title='"+detail+"'>"+data[0][i].fangm;
		
		newTd = newTr.insertCell(); 
		if(data[0][i].zhuz!=null){
			newTd.innerHTML=""+data[0][i].zhuz;
		}
	}
}
